<?php

namespace app\common\job;

use app\common\service\TransfersServer;
use app\common\traits\JobRetryTraits;
use think\queue\Job;

/**
 * 批量转账 单笔结果 查询
 * Class TransferBatchResultQuery
 * @package app\common\job
 */
class TransferBatchResultQuery
{
    use JobRetryTraits;

    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        $id = $data['id'] ?? null;

        if (!is_id($id)) {
            $job->delete();
            return;
        }

        $result = $this->getTransfersServer()->resultQuery($id);

        // 没有查询到结果
        if (is_null($result)) {
            $this->retryUntilMaxAttempts($job, 24, fn($attempts) => pow(min($attempts, 3), 2) * 1200, '消费队列：查询转账结果重试次数达到最大 ID=' . $id);
            return;
        }

        $job->delete();
    }

    private function getTransfersServer(): TransfersServer
    {
        return app(TransfersServer::class);
    }
}
